package comp418.blingo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
/**
 * 
 * @author Jingbang
 *
 */
public class question extends Activity {
	private Utility utility=null;
	private ArrayList<QButton> bt_question=null;
	private Button bt_update;
	private Button bt_create;
	private Context mContext;
	TableLayout questionList;
	private int qnumber;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.question);
		utility=Utility.getUtility();
		mContext=this;
		questionList=(TableLayout) findViewById(R.id.questionlayout);
		qnumber=0;
		//TODO: update questionlish
		ArrayList<QuestionShow> qs=utility.showList();
		qnumber=qs.size();
		bt_update=(Button) findViewById(R.id.updateQuestion);
		bt_create=(Button) findViewById(R.id.createquestion);
		bt_update.setOnClickListener(new bt_up());
		bt_create.setOnClickListener(new bt_createQ());
		utility.showToast(this, utility.getUsername());
		bt_question=new ArrayList<QButton>();
		for(int i=0;i<qnumber;i++){
			QButton t=new QButton(mContext,qs.get(i).questionID);
			bt_question.add(t);
			String title=qs.get(i).questionTitle;
			t.setOnClickListener(new bt_detail());
			t.setWidth(800);
			t.setMaxLines(2);
		t.setText(title);
			
			TableRow r=new TableRow(mContext);
			r.addView(t);
			questionList.addView(r);
			
		}
	
	}
	
	class bt_detail implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			int currentID=0;
			QButton temp=(QButton) v;
			currentID=temp.id;
			utility.setQid(currentID);	//set current viewing question to be qid
			
			Intent intent=new Intent();
			String user=utility.getUsername();
			intent.putExtra("username", user);
			intent.setClass(question.this,description.class);
			
			question.this.startActivity(intent);

		}
	}
	
	class bt_up implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			ArrayList<QuestionShow> qs=utility.showList();
			qnumber=qs.size();
			questionList.removeAllViews();
			for(int i=0;i<qnumber;i++){
				QButton t=new QButton(mContext,qs.get(i).questionID);
				bt_question.add(t);
				String title=qs.get(i).questionTitle;
				t.setOnClickListener(new bt_detail());
				t.setText(title);
				TableRow r=new TableRow(mContext);
				r.addView(t);
				questionList.addView(r);
				
			}
		
		}
	}
	

	class bt_createQ implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			//greate question
			Intent intent=new Intent();
			String user=utility.getUsername();
			intent.putExtra("username", user);
			intent.setClass(question.this,create.class);
			
			question.this.startActivity(intent);
			
		
		}
	}
}
